/*
 * @Author: your name
 * @Date: 2021-11-21 13:10:56
 * @LastEditTime: 2021-11-21 13:10:56
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\admin\src\utils\oss.js
 */
const OSS = require("ali-oss");
//config.js
//阿里云oss上传配置(*号根据自己的信息替换)
var client = new OSS({
    region: "oss-cn-shanghai", //oss地址
    accessKeyId: "LTAI5tMgG5E6MJKiKdamXTTF", //accessKeyId
    accessKeySecret: "hPk35wtyuEufQph2ulrdbcPZK02hPN", //secret
    secure: true,
    bucket: "lzw929", //oss名字
    timeout: "60s", //设置响应时间
});
/**
 *  上传文件，大小不能超过5GB
 * @param {string} ObjName OSS的储存路径和文件名字
 * @param {string} fileUrl 本地文件
 * @retruns Promise
 */
export const put = async(ObjName, fileUrl) => {
    try {
        let result = await client.put(`${ObjName}`, fileUrl);
        // ObjName为文件名字,可以只写名字，就直接储存在 bucket 的根路径，如需放在文件夹下面直接在文件名前面加上文件夹名称
        return result;
    } catch (e) {
        console.log(e);
    }
};

// 上传成功之后，转换真实的地址
export const signatureUrl = async(ObjName) => {
    try {
        let result = await client.signatureUrl(`${ObjName}`);
        return result;
    } catch (e) {
        console.log(e);
    }
};

export const getFileNameUUID = () => {
    function rx() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    }
    return `${+new Date()}_${rx()}${rx()}`;
};
//获取oss数据列表
export const list = async() => {
    try {
        let result = await client.list();
        return result;
    } catch (err) {
        console.log(err);
    }
};
//删除oss数据列表
export const del = async(fileName) => {
    try {
        let result = await client.delete(fileName);
        return result;
    } catch (err) {
        console.log(err);
    }
};